#include "PathDrawer.h"

PathDrawer::PathDrawer(GraphDrawer* drawer)
{
	this->pathSurface = SDL_CreateRGBSurface(SDL_HWSURFACE, 1024, 768, 32, 0, 0, 0, 0);
	this->drawer = drawer;
}

PathDrawer::~PathDrawer(void)
{
}

SDL_Surface* PathDrawer::drawPath(Map* map, List* vertexListPath, string idEdgeFrom, int addFrom, string idEdgeTo, int addTo) {
	drawer->fillSurface(this->pathSurface, Color::white);
	if(vertexListPath != null) {
		Edge *edgefrom = map->getGraph()->getEdge(idEdgeFrom);
		Edge *edgeTo = map->getGraph()->getEdge(idEdgeTo);

		if (edgefrom->getIdTo().compare(edgeTo->getIdFrom()) == 0 || vertexListPath->getSize() > 0) {

			Vertex *origin = map->getGraph()->getVertex(edgefrom->getIdFrom().c_str());
			Vertex *dentiny = map->getGraph()->getVertex(edgefrom->getIdTo().c_str());
			float ox=origin->getX();
			float oy=origin->getY();

			float dx=dentiny->getX();
			float dy=dentiny->getY();

			float xInicio = (( (dx - ox) / ( abs( edgefrom->getLastAddress() - edgefrom->getInitAddress() ) ) ) * (abs (addFrom - edgefrom->getInitAddress())) )+ox;
			float yInicio = (( (dy - oy) / ( abs( edgefrom->getLastAddress() - edgefrom->getInitAddress() ) ) ) * (abs (addFrom - edgefrom->getInitAddress())) )+oy;

			drawer->drawIMG(drawer->getOriginDot(), drawer->fixXPosition(xInicio) + 15,drawer->fixYPosition(yInicio) + 15, pathSurface);

			drawer->drawLine(pathSurface,drawer->fixXPosition(xInicio),drawer->fixYPosition(yInicio),drawer->fixXPosition(dx),drawer->fixYPosition(dy));
				
			for (int i = vertexListPath->getSize() - 1; i > 0; i-- ) {
				float xo =  ((VertexNode*)vertexListPath->get(i))->getNodeValue()->getX();
				float yo =	((VertexNode*)vertexListPath->get(i))->getNodeValue()->getY();
				float xf =  ((VertexNode*)vertexListPath->get(i-1))->getNodeValue()->getX();
				float yf =	((VertexNode*)vertexListPath->get(i-1))->getNodeValue()->getY();

				drawer->drawLine(pathSurface,drawer->fixXPosition(xo),drawer->fixYPosition(yo),drawer->fixXPosition(xf),drawer->fixYPosition(yf));
			}

			origin = map->getGraph()->getVertex(edgeTo->getIdFrom().c_str());
			dentiny = map->getGraph()->getVertex(edgeTo->getIdTo().c_str());
			ox=origin->getX();
			oy=origin->getY();

			dx=dentiny->getX();
			dy=dentiny->getY();

			xInicio = (( (dx - ox) / ( abs( edgeTo->getLastAddress() - edgeTo->getInitAddress() ) ) ) * (abs (addTo - edgeTo->getInitAddress())) )+ox;
			yInicio = (( (dy - oy) / ( abs( edgeTo->getLastAddress() - edgeTo->getInitAddress() ) ) ) * (abs (addTo - edgeTo->getInitAddress())) )+oy;

			drawer->drawLine(pathSurface,drawer->fixXPosition(ox),drawer->fixYPosition(oy),drawer->fixXPosition(xInicio),drawer->fixYPosition(yInicio));

			drawer->drawIMG(drawer->getDestinyDot(), drawer->fixXPosition(xInicio) + 15,drawer->fixYPosition(yInicio) + 15, pathSurface);
		}
	} else {
		if (idEdgeFrom.compare(idEdgeTo) == 0) {
			Edge *edgefrom = map->getGraph()->getEdge(idEdgeFrom);
			Vertex *origin = map->getGraph()->getVertex(edgefrom->getIdFrom().c_str());
			Vertex *dentiny = map->getGraph()->getVertex(edgefrom->getIdTo().c_str());

			float ox=origin->getX();
			float oy=origin->getY();

			float dx=dentiny->getX();
			float dy=dentiny->getY();

			float xInicio = (( (dx - ox) / ( abs( edgefrom->getLastAddress() - edgefrom->getInitAddress() ) ) ) * (abs (addFrom - edgefrom->getInitAddress())) )+ox;
			float yInicio = (( (dy - oy) / ( abs( edgefrom->getLastAddress() - edgefrom->getInitAddress() ) ) ) * (abs (addFrom - edgefrom->getInitAddress())) )+oy;

			float xTo = (( (dx - ox) / ( abs( edgefrom->getLastAddress() - edgefrom->getInitAddress() ) ) ) * (abs (addTo - edgefrom->getInitAddress())) )+ox;
			float yTo = (( (dy - oy) / ( abs( edgefrom->getLastAddress() - edgefrom->getInitAddress() ) ) ) * (abs (addTo - edgefrom->getInitAddress())) )+oy;

			drawer->drawIMG(drawer->getOriginDot(), drawer->fixXPosition(xInicio) + 15,drawer->fixYPosition(yInicio) + 15, pathSurface);
			drawer->drawLine(pathSurface,drawer->fixXPosition(xInicio),drawer->fixYPosition(yInicio),drawer->fixXPosition(xTo),drawer->fixYPosition(yTo));
			drawer->drawIMG(drawer->getDestinyDot(), drawer->fixXPosition(xTo) + 15,drawer->fixYPosition(yTo) + 15, pathSurface);
		}
	}
	return this->pathSurface;
}
